/*
 * @Autor: Ruohong Yu
 * @Date: 2022-09-02 15:13:12
 * @Description: file content
 * @FilePath: \ebook\src\utils\localStorage.js
 */

import Storage from 'web-storage-cache'

const localStorage = new Storage()

export function getLocalStorage (key) {
  return localStorage.get(key)
}

export function setLocalStorage (key, value, expire = 30 * 24 * 3600) {
  return localStorage.set(key, value, { exp: expire })
}

export function removeLocalStorage (key) {
  return localStorage.delete(key)
}

export function clearLocalStorage () {
  return localStorage.clear()
}

export function saveAvatar (data) {
  return setLocalStorage('avatar', data)
}
export function getAvatar () {
  return getLocalStorage('avatar')
}

export function saveToken (data) {
  return setLocalStorage('token', data)
}
export function getToken () {
  return getLocalStorage('token')
}

export function saveUserEmail (data) {
  return setLocalStorage('useremail', data)
}
export function getUserEmail () {
  return getLocalStorage('useremail')
}

export function saveUsername (data) {
  return setLocalStorage('username', data)
}
export function getUsername () {
  return getLocalStorage('username')
}

export function saveHistorySearchList (data) {
  return setLocalStorage('historySearchList', data)
}
export function getHistorySearchList () {
  return getLocalStorage('historySearchList')
}

export function saveIsFirst (data) {
  return setLocalStorage('isFirst', data)
}
export function getIsFirst () {
  return getLocalStorage('isFirst')
}

export function saveIsOpenCamera (data) {
  return setLocalStorage('isOpenCamera', data)
}
export function getIsOpenCamera () {
  return getLocalStorage('isOpenCamera')
}

export function saveBookShelf (shelf) {
  return setLocalStorage('shelf', shelf)
}
export function getBookShelf () {
  return getLocalStorage('shelf')
}

export function getHome () {
  return getLocalStorage('home')
}

export function saveHome (home) {
  return setLocalStorage('home', home, 1800)
}

export function getLocale () {
  return getLocalStorage('locale')
}

export function saveLocale (locale) {
  return setLocalStorage('locale', locale)
}

export function getLocation (fileName) {
  return getBookObject(fileName, 'location')
}

export function saveLocation (fileName, location) {
  setBookObject(fileName, 'location', location)
}

export function getBookmark (fileName) {
  return getBookObject(fileName, 'bookmark')
}

export function saveBookmark (fileName, bookmark) {
  setBookObject(fileName, 'bookmark', bookmark)
}

export function getReadTime (fileName) {
  return getBookObject(fileName, 'time')
}

export function saveReadTime (fileName, theme) {
  setBookObject(fileName, 'time', theme)
}

export function getProgress (fileName) {
  return getBookObject(fileName, 'progress')
}

export function saveProgress (fileName, progress) {
  setBookObject(fileName, 'progress', progress)
}

export function getNavigation (fileName) {
  return getBookObject(fileName, 'navigation')
}

export function saveNavigation (fileName, navigation) {
  setBookObject(fileName, 'navigation', navigation)
}

export function getMetadata (fileName) {
  return getBookObject(fileName, 'metadata')
}

export function saveMetadata (fileName, metadata) {
  setBookObject(fileName, 'metadata', metadata)
}

export function getCover (fileName) {
  return getBookObject(fileName, 'cover')
}

export function saveCover (fileName, cover) {
  setBookObject(fileName, 'cover', cover)
}

export function getFontFamily (fileName) {
  return getBookObject(fileName, 'fontFamily')
}

export function saveFontFamily (fileName, fontFamily) {
  setBookObject(fileName, 'fontFamily', fontFamily)
}

export function getTheme (fileName) {
  return getBookObject(fileName, 'theme')
}

export function saveTheme (fileName, theme) {
  setBookObject(fileName, 'theme', theme)
}

export function getFontSize (fileName) {
  return getBookObject(fileName, 'fontSize')
}

export function saveFontSize (fileName, fontSize) {
  setBookObject(fileName, 'fontSize', fontSize)
}

export function getBookObject (fileName, key) {
  if (getLocalStorage(`${fileName}-info`)) {
    return getLocalStorage(`${fileName}-info`)[key]
  } else {
    return null
  }
}

export function setBookObject (fileName, key, value) {
  let book = {}
  if (getLocalStorage(`${fileName}-info`)) {
    book = getLocalStorage(`${fileName}-info`)
  }
  book[key] = value
  setLocalStorage(`${fileName}-info`, book)
}
